package com.xmall.auth.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.auth.entity.SysMenu;
import com.xmall.auth.entity.SysPermission;
import com.xmall.auth.entity.SysResource;
import com.xmall.auth.entity.SysRole;
import com.xmall.auth.service.ISysRoleService;
import com.xmall.common.config.CommonPage;
import com.xmall.common.config.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author liuzh
 * @since 2020-06-14
 */
@RestController
@RequestMapping("/role" )
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult registerMenu(@RequestBody SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRole.setAdminCount(0);
        sysRole.setSort(0);
        boolean save = sysRoleService.saveSysRole(sysRole);
        if (!save) {
            return CommonResult.failed();
        }
        return CommonResult.success(save);
    }


    @ApiOperation("删除后台用户角色表ById" )
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult delete(@PathVariable Long id) {
        boolean delete = sysRoleService.deleteSysRole(id);
        if (!delete) {
            return CommonResult.failed();
        }
        return CommonResult.success(delete);
    }


    @ApiOperation("批量删除角色" )
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids" ) List<Long> ids) {
        int count = sysRoleService.deleteSysRoleIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新后台用户角色表" )
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody SysRole sysRole) {
        boolean update = sysRoleService.updateSysRole(id, sysRole);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success(update);
    }

    @ApiOperation("获取后台用户角色表ById" )
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SysRole> getSysRole(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getSysRole(id);
        return CommonResult.success(sysRole);
    }


    @ApiOperation("分页查询后台用户角色表" )
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SysRole>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                  @RequestParam(value = "pageSize", defaultValue = "5" ) Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1" ) Integer pageNum) {

        Page<SysRole> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(keyword)) {
            queryWrapper.like("user_name", keyword);
        }
        IPage<SysRole> pageList = sysRoleService.page(page, queryWrapper);
        return CommonResult.success(CommonPage.restPage(pageList));
    }


    @ApiOperation("获取相应角色权限" )
    @RequestMapping(value = "/permission/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SysPermission>> getPermissionList(@PathVariable Long roleId) {
        List<SysPermission> permissionList = sysRoleService.getPermissionList(roleId);
        return CommonResult.success(permissionList);
    }

    @ApiOperation("修改角色权限" )
    @RequestMapping(value = "/permission/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatePermission(@RequestParam Long roleId,
                                         @RequestParam("permissionIds" ) List<Long> permissionIds) {
        int count = sysRoleService.updatePermission(roleId, permissionIds);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取所有角色" )
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SysRole>> listAll() {
        List<SysRole> roleList = sysRoleService.list();
        return CommonResult.success(roleList);
    }

//    @ApiOperation("修改角色状态")
//    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {
//        SysRole umsRole = new SysRole();
//        umsRole.setStatus(status);
//        int count = sysRoleService.update(id, umsRole);
//        if (count > 0) {
//            return CommonResult.success(count);
//        }
//        return CommonResult.failed();
//    }

    @ApiOperation("获取角色相关菜单" )
    @RequestMapping(value = "/listMenu/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SysMenu>> listMenu(@PathVariable Long roleId) {
        List<SysMenu> roleList = sysRoleService.listMenu(roleId);
        return CommonResult.success(roleList);
    }

    @ApiOperation("获取角色相关资源" )
    @RequestMapping(value = "/listResource/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SysResource>> listResource(@PathVariable Long roleId) {
        List<SysResource> roleList = sysRoleService.listResource(roleId);
        return CommonResult.success(roleList);
    }

    @ApiOperation("给角色分配菜单" )
    @RequestMapping(value = "/allocMenu", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        int count = sysRoleService.allocMenu(roleId, menuIds);
        return CommonResult.success(count);
    }

    @ApiOperation("给角色分配资源" )
    @RequestMapping(value = "/allocResource", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        int count = sysRoleService.allocResource(roleId, resourceIds);
        return CommonResult.success(count);
    }

}

